package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiniu.util.Md5;
import com.sc.nft.dao.CollectionChipInfoDao;
import com.sc.nft.dto.CollectionChipInfoPageDTO;
import com.sc.nft.dto.CollectionChipInfoRecordPageDTO;
import com.sc.nft.entity.CollectionChipInfo;
import com.sc.nft.entity.vo.CollectionChipInfoPageVO;
import com.sc.nft.entity.vo.CollectionChipInfoRecordPageVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.CollectionChipInfoService;
import com.sc.nft.service.CollectionChipService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Service
public class CollectionChipInfoServiceImpl implements CollectionChipInfoService {
    private final CollectionChipInfoDao collectionChipInfoDao;

    /**
     * 芯片列表
     * @param pageDTO
     * @return
     */
    @Override
    public Page<CollectionChipInfoPageVO> page(CollectionChipInfoPageDTO pageDTO) {
        return collectionChipInfoDao.pageList(pageDTO);
    }

    /**
     * 识别列表
     *
     * @param pageDTO
     * @return
     */
    @Override
    public IPage<CollectionChipInfoRecordPageVO> recordPage(CollectionChipInfoRecordPageDTO pageDTO) {
        return collectionChipInfoDao.recordPage(pageDTO);
    }

    /**
     * 芯片信息导出
     *
     * @param pageDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> export(CollectionChipInfoPageDTO pageDTO) {
        List<String> exportList = new ArrayList<>();
        List<CollectionChipInfo> chipInfoList = collectionChipInfoDao.export(pageDTO);
        Assert.notEmpty(chipInfoList, () -> new GlobalRunTimeException("暂无可导出信息"));
        Assert.isTrue(collectionChipInfoDao.updateWriteStatus(chipInfoList.stream().map(CollectionChipInfo::getId).collect(Collectors.toList()), Boolean.TRUE),()->new GlobalRunTimeException("导出失败")); ;
        for (CollectionChipInfo chipInfo : chipInfoList) {
            exportList.add(Base64.getEncoder().encodeToString((chipInfo.getId() + ":" + chipInfo.getUuid()).getBytes()));
        }
        return exportList;
    }


}
